package com.sicheng.lc.周赛.分类.哈西表or计数or滑动窗口or双指针.计数;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/6/14 22:00
 */
public class 最多单词数的发件人 {
    //    https://leetcode.cn/problems/sender-with-largest-word-count/
    public String largestWordCount(String[] messages, String[] senders) {
        HashMap<String, Integer> map = new HashMap<>();
        int index = 0;
        for (String message : messages) {
            int count = 0;
            for (int i = 0; i < message.length(); i++) {
                if (message.charAt(i) == ' ') {
                    count++;
                }
            }
            count++;
            map.put(senders[index], map.getOrDefault(senders[index], 0) + count);
            index++;
        }

        int max = Integer.MIN_VALUE;
        String res = null;
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            int value = entry.getValue();
            if (value > max) {
                max = value;
            }
        }

        for (Map.Entry<String, Integer> entry : entries) {
            int value = entry.getValue();
            if (value == max) {
                String key = entry.getKey();
                res = res == null ? key : (key.compareTo(res) < 0 ? res : key);
            }
        }

        return res;
    }
}
